#include "head.h"

MusicPlayer::MusicPlayer()
{
    resize(Widget::MAINWIDGET_WIDTH, Widget::MAINWIDGET_HEIGHT);
    setAttribute(Qt::WA_TranslucentBackground, true);
    _musicInst = libvlc_new(0, nullptr);
    _timer = new QTimer(this);
    connect(_timer, SIGNAL(timeout()), this, SLOT(update()));
    if(_musicInst == nullptr)
    {
        qDebug() << "_musicInst init error!";
    }
    _musicMediaPalyer = nullptr;
    setAcceptDrops(true);
    if(_musicInst == nullptr)
    {
        qDebug() << "_musicInst is null" ;
    }
//    setStyleSheet("QLabel {background-color: white;}");//设置样式
//        setAutoFillBackground(true);//执行填充
    loadWidget();
}


void MusicPlayer::update()
{
    auto pos = libvlc_media_player_get_position(_musicMediaPalyer);
    if(pos == -1)
    {
        qDebug() << libvlc_errmsg();
    }
    if(libvlc_media_player_get_state(_musicMediaPalyer) == libvlc_Ended)
    {
        _timer->stop();
        libvlc_media_player_stop(_musicMediaPalyer);

        libvlc_media_player_set_position(_musicMediaPalyer, 0);
//        libvlc_media_player_play(_musicMediaPalyer);

        emit resetToBegin();
        return;
    }
    emit sendMusicProgress(pos);
}

void MusicPlayer::changeStatue(bool statuPlay)
{
    if(_musicMediaPalyer == nullptr)
    {
        return;
    }
    if(statuPlay)
    {
        libvlc_media_player_play(_musicMediaPalyer);
 //       libvlc_state_t flag = libvlc_media_player_get_state(_musicMediaPalyer);
//        qDebug() << flag << i;
        _timer->start(35);
    }else{
//        libvlc_state_t flag = libvlc_media_player_get_state(_musicMediaPalyer);
//        qDebug() << flag;
        libvlc_media_player_pause(_musicMediaPalyer);

        _timer->stop();
    }
}

void MusicPlayer::loadWidget()
{
    auto vu = new QVBoxLayout();
    vu->setSpacing(0);
    vu->setContentsMargins(0,7,0,0);

    auto musicProgress = new ProgressBar();
    connect(this, SIGNAL(sendMusicLenthToProgressBar(double)), musicProgress, SLOT(acceptMusicLength(double)));
    vu->addWidget(musicProgress, 0/*, Qt::AlignTop*/);

    auto musicCDPlayer = new CdPlayer();
    QPoint p1 = QPoint(0, musicProgress->pos().y() + musicProgress->height());
    QPoint p2 = QPoint(440, 144 - (musicProgress->pos().y() + musicProgress->height()));
    musicCDPlayer->initPoint(p1, p2);

    connect(this, SIGNAL(resetToBegin()), musicCDPlayer, SLOT(acceptResetBegin()));
    connect(musicCDPlayer, SIGNAL(sendMusicProgress(double)), musicProgress, SLOT(acceptCdMusicProgress(double)));
    connect(musicProgress, SIGNAL(sendCdMusicProgress(double)), musicCDPlayer, SLOT(acceptMusicProgress(double)));
    connect(musicProgress, SIGNAL(sendVlcPlayerProgress(double)), this, SLOT(changeVlcProgress(double)));
    connect(musicCDPlayer, SIGNAL(sendVlcProgress(double)), this, SLOT(changeVlcProgress(double)));

    vu->addWidget(musicCDPlayer, 0);

    auto controlButton = new ControlButton();
    _controlButton = controlButton;
    connect(controlButton, SIGNAL(sendChangeStatueToMusicPlayer(bool)), this, SLOT(changeStatue(bool)));
    connect(this, SIGNAL(resetToBegin()), controlButton, SLOT(acceptResetBegin()));
    connect(this, SIGNAL(sendMusicProgress(double)), musicCDPlayer, SLOT(acceptMusicProgress(double)));
    connect(this, SIGNAL(sendMusicProgress(double)), musicProgress, SLOT(acceptCdMusicProgress(double)));

    vu->addWidget(controlButton, 0);

    auto volumnContral = new VolumeControl();
    _volumnContral = volumnContral;
    connect(volumnContral, SIGNAL(changeVolume(double)), this, SLOT(changeVolume(double)));
    vu->addWidget(volumnContral, 0);

    setLayout(vu);

    //    auto hb = new QHBoxLayout();
    //    vu->addLayout(hb);

    //    auto labelTimingLeft =  new QLabel();
    //    labelTimingLeft->resize(50,25);
    //    labelTimingLeft->setFont(QFont("Arial",18,50));
    //    labelTimingLeft->setText("00:00");
    //    hb->addWidget(labelTimingLeft, 0, Qt::AlignLeft);



    //    auto labelTimeRight = new QLabel();
    //    labelTimeRight->resize(50, 25);
    //    labelTimingLeft->setFont(QFont("Arial",18,50));
    //    labelTimingLeft->setText("00:00");
    //    hb->addWidget(labelTimeRight,0,Qt::AlignRight);
}

void MusicPlayer::changeVolume(double ratio)
{
    if(_musicMediaPalyer != nullptr)
    {
        libvlc_audio_set_volume(_musicMediaPalyer, static_cast<int>(ratio * 100));
    }
}

void MusicPlayer::changeVlcProgress(double ratio)
{
    if(_musicMediaPalyer != nullptr)
    {
        libvlc_media_player_set_position(_musicMediaPalyer, static_cast<float>(ratio));
    }
}

bool MusicPlayer::upDateSong(QString musicPath)
{
//    auto tt = musicPath.toLatin1();
    musicPath = QDir::toNativeSeparators(musicPath);//从mimedate中获取的文件路径默认是linux模式，需要反转分隔符，变成本地模式
    auto media = libvlc_media_new_path(_musicInst, /*tt.constData()*/musicPath.toUtf8().constData());

    if(media == nullptr)
    {
        qDebug() << libvlc_errmsg() << musicPath;
        return false;
    }
    if(_musicMediaPalyer != nullptr)
    {
        libvlc_media_player_release(_musicMediaPalyer);
    }
    libvlc_media_parse(media);//要先解析否者会有段错误
    _musicMediaPalyer = libvlc_media_player_new_from_media(media);
    if(_musicMediaPalyer == nullptr)
    {
        qDebug() << "_musicMediaPalyer init error";
        return false;
    }

    QString artistName = libvlc_media_get_meta(media, libvlc_meta_Artist);
    QString songName = libvlc_media_get_meta(media, libvlc_meta_Title);
    double songLenth = libvlc_media_get_duration(media)/1000.00;
    emit sendMusicInformationToTop(songName, artistName);
    emit sendMusicLenthToProgressBar(songLenth);
    auto volumeSize = _volumnContral->GetVolumeRatio();

    libvlc_audio_set_volume(_musicMediaPalyer, static_cast<int>(volumeSize * 100));
    emit resetToBegin();

    libvlc_media_release(media);
//    libvlc_media_player_play(_musicMediaPalyer);
    return true;
}



void MusicPlayer::dragEnterEvent(QDragEnterEvent *event)
{
    auto fileUrl = event->mimeData()->urls().first().toLocalFile();
    if(fileUrl.right(3) == "mp3")
    {
       event->acceptProposedAction();
    }else{
        event->ignore();
    }
}
void MusicPlayer::dropEvent(QDropEvent *event)
{
    auto fileUrl = event->mimeData()->urls().first().toLocalFile();
    upDateSong(fileUrl);
    _controlButton->setFirstLoad(true);
}




MusicPlayer::~MusicPlayer()
{
    if(_musicInst != nullptr)
    {
        libvlc_release(_musicInst);
    }
}
